package logic

import (
	"context"
	"houseRpc/models"

	"houseRpc/house"
	"houseRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListUserHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListUserHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListUserHouseLogic {
	return &ListUserHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListUserHouseLogic) ListUserHouse(in *house.ListUserHouseRequest) (*house.ListUserHouseResponse, error) {
	// todo: add your logic here and delete this line

	var u models.User
	var ur models.RealName
	var h models.House
	var userHouse []models.House
	var userHouseList []*house.ListUserHouse

	h.UserId = in.UserId
	models.DB.Where("user_id=?", in.UserId).Find(&userHouse)
	u.ID = uint(in.UserId)
	models.DB.Where("id=?", in.UserId).Find(&u)
	ur.UserId = in.UserId
	models.DB.Where("user_id=?", in.UserId).Find(&ur)

	for _, hs := range userHouse {
		list := house.ListUserHouse{
			HouseId: int64(hs.ID),
			Title:   hs.Title,
			Ads:     hs.Ads,
			Square:  hs.Square,
			Floor:   hs.Floor,
			Price:   float32(hs.Price),
		}
		userHouseList = append(userHouseList, &list)
	}

	return &house.ListUserHouseResponse{
		UserId:   in.UserId,
		Mobile:   u.Mobile,
		RealName: ur.RealName,
		IdCard:   ur.IdCard,
		List:     userHouseList,
	}, nil
}
